﻿using System;
using System.Windows.Media;

namespace Gmantis.Controls.Chart
{
    internal class VfxTools
    {
        // Methods
        private static PathFigure CloneFigure(PathFigure pf)
        {
            PathFigure figure = new PathFigure
            {
                StartPoint = pf.StartPoint,
                IsClosed = pf.IsClosed,
                IsFilled = pf.IsFilled
            };
            PathSegmentCollection segments = pf.Segments;
            if (segments != null)
            {
                int count = segments.Count;
                for (int i = 0; i < count; i++)
                {
                    figure.Segments.Add(CloneSegment(segments[i]));
                }
            }
            return figure;
        }

        public static Geometry CloneGeometry(Geometry geom)
        {
            Transform transform = geom.Transform;
            Transform transform2 = null;
            if (transform is TranslateTransform)
            {
                TranslateTransform transform3 = (TranslateTransform)transform;
                TranslateTransform transform4 = new TranslateTransform
                {
                    X = transform3.X,
                    Y = transform3.Y
                };
                transform2 = transform4;
            }
            if (geom is RectangleGeometry)
            {
                RectangleGeometry geometry = (RectangleGeometry)geom;
                return new RectangleGeometry { RadiusX = geometry.RadiusX, RadiusY = geometry.RadiusY, Rect = geometry.Rect, Transform = transform2 };
            }
            if (geom is EllipseGeometry)
            {
                EllipseGeometry geometry3 = (EllipseGeometry)geom;
                return new EllipseGeometry { Center = geometry3.Center, RadiusX = geometry3.RadiusX, RadiusY = geometry3.RadiusY, Transform = transform2 };
            }
            if (!(geom is PathGeometry))
            {
                return null;
            }
            PathGeometry geometry5 = (PathGeometry)geom;
            PathGeometry geometry6 = new PathGeometry
            {
                FillRule = geometry5.FillRule,
                Transform = transform2
            };
            if (geometry5.Figures != null)
            {
                int count = geometry5.Figures.Count;
                for (int i = 0; i < count; i++)
                {
                    geometry6.Figures.Add(CloneFigure(geometry5.Figures[i]));
                }
            }
            return geometry6;
        }

        private static PointCollection ClonePoints(PointCollection pts)
        {
            PointCollection points = null;
            if (pts != null)
            {
                int count = pts.Count;
                points = new PointCollection();
                for (int i = 0; i < count; i++)
                {
                    points.Add(pts[i]);
                }
            }
            return points;
        }

        private static PathSegment CloneSegment(PathSegment ps)
        {
            LineSegment segment = ps as LineSegment;
            if (segment != null)
            {
                return new LineSegment { Point = segment.Point };
            }
            ArcSegment segment3 = ps as ArcSegment;
            if (segment3 != null)
            {
                return new ArcSegment { IsLargeArc = segment3.IsLargeArc, Point = segment3.Point, RotationAngle = segment3.RotationAngle, Size = segment3.Size, SweepDirection = segment3.SweepDirection };
            }
            PolyLineSegment segment5 = ps as PolyLineSegment;
            if (segment5 != null)
            {
                return new PolyLineSegment { Points = ClonePoints(segment5.Points) };
            }
            BezierSegment segment7 = ps as BezierSegment;
            if (segment7 != null)
            {
                return new BezierSegment { Point1 = segment7.Point1, Point2 = segment7.Point2, Point3 = segment7.Point3 };
            }
            QuadraticBezierSegment segment9 = ps as QuadraticBezierSegment;
            if (segment9 != null)
            {
                return new QuadraticBezierSegment { Point1 = segment9.Point1, Point2 = segment9.Point2 };
            }
            PolyBezierSegment segment11 = ps as PolyBezierSegment;
            if (segment11 != null)
            {
                return new PolyBezierSegment { Points = ClonePoints(segment11.Points) };
            }
            PolyQuadraticBezierSegment segment13 = ps as PolyQuadraticBezierSegment;
            if (segment13 == null)
            {
                throw new NotSupportedException(string.Format("{0} is not supported.", ps));
            }
            return new PolyQuadraticBezierSegment { Points = ClonePoints(segment13.Points) };
        }

        internal static bool IsFilled(Geometry geom)
        {
            if (!(geom is PathGeometry))
            {
                return true;
            }
            PathGeometry geometry = (PathGeometry)geom;
            if (geometry.Figures != null)
            {
                foreach (PathFigure figure in geometry.Figures)
                {
                    if (figure.IsFilled)
                    {
                        return true;
                    }
                }
            }
            return false;
        }
    }
}
